//
//  HYPhotoBottomView.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/12.
//

#import "HYPhotoBottomView.h"
#import <HYUIKit/UIView+Animation.h>

@interface HYPhotoBottomView ()

@property (nonatomic, strong) UIColor *iconThemeColor;

@property (nonatomic, strong) UIButton *finishBtn;

@property (nonatomic, strong) UIImageView *numberImageView;

@property (nonatomic, strong) UILabel *numberLabel;

@property (nonatomic, copy) dispatch_block_t finishBlock;

@end

@implementation HYPhotoBottomView

- (instancetype)initWithFinishBlock:(dispatch_block_t)finishBlock;
{
    self = [super init];
    if(self){
        self.backgroundColor = AppContentBgThemeColor;
        self.finishBlock = finishBlock;
        self.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
        [self addSubview:self.finishBtn];
        [self addSubview:self.numberImageView];
        [self addSubview:self.numberLabel];
        [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-12);
            make.top.mas_equalTo(0);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(44);
        }];
        [self.numberImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.finishBtn.mas_left).offset(-5);
            make.centerY.equalTo(self.finishBtn.mas_centerY);
            make.width.height.mas_equalTo(24);
        }];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.numberImageView.mas_centerX);
            make.centerY.equalTo(self.finishBtn.mas_centerY);
            make.width.height.mas_equalTo(24);
        }];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doneButtonClick)];
        [self.numberLabel addGestureRecognizer:tapGesture];
    }
    return self;
}


- (void)setSelectCount:(NSInteger)selectCount
{
    _selectCount = selectCount;
    self.numberLabel.hidden = selectCount?NO:YES;
    self.numberImageView.hidden = selectCount?NO:YES;
    self.finishBtn.enabled = selectCount;
    if(selectCount){
        self.numberLabel.text = [NSString stringWithFormat:@"%zd",selectCount];
        [UIView showOscillatoryAnimationWithLayer:self.numberImageView.layer oscillatoryType:HYOscillatoryAnimation_Bigger];
    }
}

- (void)setOKButtonTitleColorNormal:(UIColor *)oKButtonTitleColorNormal
{
    _oKButtonTitleColorNormal = oKButtonTitleColorNormal;
    [self.finishBtn setTitleColor:self.oKButtonTitleColorNormal forState:UIControlStateNormal];
    
}

- (void)setOKButtonTitleColorDisabled:(UIColor *)oKButtonTitleColorDisabled
{
    _oKButtonTitleColorDisabled = oKButtonTitleColorDisabled;
    [self.finishBtn setTitleColor:self.oKButtonTitleColorDisabled forState:UIControlStateDisabled];
}

- (void)doneButtonClick
{
    if(self.finishBlock){
        self.finishBlock();
    }
}

- (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size radius:(CGFloat)radius
{
    if (!color) {
        color = self.iconThemeColor;
    }
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];
    CGContextAddPath(context, path.CGPath);
    CGContextFillPath(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - loadLoazy
- (UIButton *)finishBtn
{
    if(!_finishBtn){
        _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _finishBtn.enabled = NO;
        [_finishBtn setTitle:NSLocalizedString(@"完成", nil) forState:UIControlStateNormal];
        [_finishBtn setTitle:NSLocalizedString(@"完成", nil) forState:UIControlStateDisabled];
        _finishBtn.titleLabel.font = FONT_BOLD_SIZE(16);
        [_finishBtn addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _finishBtn;
}

- (UIImageView *)numberImageView
{
    if(!_numberImageView){
        _numberImageView = [[UIImageView alloc]init];
        _numberImageView.hidden = YES;
        _numberImageView.image = [self createImageWithColor:nil size:CGSizeMake(24, 24) radius:12];
        _numberImageView.contentMode = UIViewContentModeScaleAspectFit;
        _numberImageView.clipsToBounds = YES;
        _numberImageView.backgroundColor = kClearColor;
    }
    return _numberImageView;
}

- (UILabel *)numberLabel
{
    if(!_numberLabel){
        _numberLabel = [[UILabel alloc]init];
        _numberLabel.hidden = YES;
        _numberLabel.font = FONT_SIZE(15);
        _numberLabel.textColor = kWhiteColor;
        _numberLabel.adjustsFontSizeToFitWidth = YES;
        _numberLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _numberLabel;
}

@end
